﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Linq;

namespace RSSR
{
    public partial class MainPage : PhoneApplicationPage
    {
        string _RSSFeedsStore = "Feeds.xml";
        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            //store.DeleteFile(_RSSFeedsStore);
            
            // Check Config file is there, if not create it and add the config item.
            if (!ConfigFile.ConfigFileExists()) CreateConfigFile();
                
            // Loading Feeds
            // Opening our store
            IsolatedStorageFileStream MyConfigFile = store.OpenFile(_RSSFeedsStore, System.IO.FileMode.Open);
            
            // parding the XML document
            XElement MyXMLConfig = XElement.Load(MyConfigFile);

            // Using LINQ to XML to fill our listbox.
            lbFeeds.ItemsSource = from RSSFeeds in MyXMLConfig.Descendants("RSSFeed")
                                  orderby RSSFeeds.Element("tag").Value
                                  select new RSSFeed
                                  {
                                      FeedTitle = RSSFeeds.Element("FeedTitle").Value,
                                      FeedDetails = RSSFeeds.Element("FeedDetails").Value,
                                      FeedURL = RSSFeeds.Element("FeedURL").Value,
                                      tag = RSSFeeds.Element("tag").Value
                                  };

        }

        private void CreateConfigFile()
        {
            
            // Build the xml config file

            ConfigFile.CreateConfigFile();
            RSSFeed WPME = new RSSFeed
            {
                    FeedURL= "http://feeds.feedburner.com/windowsphoneme", 
                    FeedTitle = "windows phone middle east", 
                    FeedDetails = "Your windows phones Development Resource", 
                    tag = ""
            };

            RSSFeed theConfigItem = new RSSFeed
            {
                FeedURL = "",
                FeedTitle = "add new RSS feed",
                FeedDetails = "please click here to configure",
                tag = "navtonewfeedpage"
            };

            ConfigFile.AddRSSFeed(WPME);
            ConfigFile.AddRSSFeed(theConfigItem);
            
        }

        private void lbFeeds_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            if (lbFeeds.SelectedItem == null)
                return;

            

            // Move to add new feed page.
            if (((RSSFeed)lbFeeds.SelectedItem).tag == "navtonewfeedpage")
            {
                NavigationService.Navigate(new Uri("/newFeed.xaml", UriKind.Relative));
            }
            else if (string.IsNullOrEmpty(((RSSFeed)lbFeeds.SelectedItem).tag)) // Move to view Feed
            {
                FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
                root.DataContext = (RSSFeed)lbFeeds.SelectedItem;
                NavigationService.Navigate(new Uri("/DetailsPage.xaml", UriKind.Relative));
            }
        }
    }
}